package no.ntnu.fp.messages.impl;

import no.ntnu.fp.messages.Message;
import no.ntnu.fp.messages.MessageCode;
import no.ntnu.fp.utils.MeetingRoomSchedule;
import no.ntnu.fp.utils.XmlParser;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class MeetingRoomsFetchedMessage implements Message {
	private final MeetingRoomSchedule schedule;
	
	public MeetingRoomsFetchedMessage(MeetingRoomSchedule schedule) {
		this.schedule = schedule;
	}

	public MeetingRoomsFetchedMessage() {
		this(null);
	}

	public MeetingRoomSchedule getSchedule() {
		return schedule;
	}

	@Override
	public MessageCode getMessageCode() {
		return MessageCode.MEETING_ROOMS_FETCHED_MESSAGE;
	}

	@Override
	public Message fromXmlString(String xml) {
		Document doc = XmlParser.parseXmlString(xml);
		MeetingRoomSchedule sch = new MeetingRoomSchedule((Element)
				doc.getElementsByTagName("schedule").item(0));
		return new MeetingRoomsFetchedMessage(sch);
	}

	@Override
	public String toString() {
		Document doc = XmlParser.getThreadLocalDocumentBuilder().newDocument();
		Element root = doc.createElement("message");
		root.appendChild(schedule.toXml(doc));
		
		return getMessageCode() + XmlParser.convertXmlToString(root);
	}

}
